package com.example.im.ui.fragment

import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import com.example.im.R
import com.example.im.bean.EventBusData
import com.example.im.common.Enumerate
import com.example.im.db.DatabaseStorage
import com.example.im.ui.adapter.pager.FriendPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.SearchActivity
import kotlinx.android.synthetic.main.fragment_main_friend.*
import kotlinx.android.synthetic.main.fragment_main_friend.linear_search
import kotlinx.android.synthetic.main.tab_layout_custom.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainFriendFragment : AbstractBaseFragment<BasePresenter<*>>(),
    TabLayout.OnTabSelectedListener {

    override val layoutId: Int
        get() = R.layout.fragment_main_friend

    override fun initView() {

        EventBus.getDefault().register(this)

        val fragments = mutableListOf<Fragment>(
            FriendUserFragment(),
            FriendRoomFragment(),
            FriendRequestFragment()
        )
        val pagerAdapter = FriendPagerAdapter(
            childFragmentManager, fragments.toMutableList()
        )
        view_pager.adapter = pagerAdapter
        tab_layout.setupWithViewPager(view_pager)
        for (index in fragments.indices) {
            tab_layout.getTabAt(index)!!.customView = pagerAdapter.getTabView(activity!!, index)
        }
        tab_layout.addOnTabSelectedListener(this)
        tab_layout.getTabAt(2)!!.customView?.tv_tab_badge?.visibility =
            if (DatabaseStorage.getUserRoomUnread() > 0) View.VISIBLE else View.GONE
        linear_search.setOnClickListener {
            activity!!.startActivity(Intent(activity, SearchActivity::class.java))
        }


    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(eventBus: EventBusData<*>) {
        when (eventBus.type ?: return) {
            Enumerate.Handle.list_subscribe -> tab_layout.getTabAt(2)!!.customView?.tv_tab_badge?.visibility =
                if (DatabaseStorage.getUserRoomUnread() > 0) View.VISIBLE else View.GONE
            else -> return
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    override fun onTabReselected(p0: TabLayout.Tab?) {

    }

    override fun onTabUnselected(p0: TabLayout.Tab?) {
        val view = p0!!.customView
        view!!.tv_tab_name.setTextColor(activity!!.resources.getColor(R.color.gray_4))
        view.tv_tab_name.setBackgroundResource(R.color.white)
    }

    override fun onTabSelected(p0: TabLayout.Tab?) {
        val view = p0!!.customView
        view!!.tv_tab_name.setTextColor(activity!!.resources.getColor(R.color.blue))
        view.tv_tab_name.setBackgroundResource(R.drawable.style_main_friend_tab_layou)
    }

}